import {createRouter, createWebHistory,createWebHashHistory} from 'vue-router';
import { useLoginStore } from '../stores/loginStore';
import { menuStore } from '../stores/menuStore'
import Home from '../views/Home.vue';
import Login from '../views/Login.vue';

const router = createRouter({
    history:createWebHashHistory(import.meta.env.BASE_URL),
    routes:[
        {
            path:'/',
            name:'home',
            component:Home,
            meta:{
                requireAuth:true
            },
            children:[
            ]
        },
        {
            path:'/onlineorder',
            name:'onlineorder',
            component:()=>import("../views/OnlineOrder.vue"),
            meta:{
                requireAuth:true
            },
            children:[
            ]
        },
        {
            path:'/login',
            name:'login',
            component:Login
        }
    ]
})

// 路由权限
router.beforeEach((to, from, next) => {
    if(to.meta.requireAuth){
        // 需要登录
        const loginStore = useLoginStore();
        if(!loginStore.token){
            next({
                path:"/login"
            });
        }else{
            next();
        }
    }else{
        next();
    }
})

router.afterEach((to, from) =>{
    // 储存路由路径
    localStorage.setItem("active",to.path)
    if(to.meta.key){
        // 储存key
        const mStore = menuStore();
        mStore.breadcrump = to.meta.key
    }
})
export default router